var Comment = require('../models/comment.model');
var moment = require('moment');


exports.create = function (req, res, next) {
    var comment = new Comment(req.body);
    comment = Object.assign(comment, {
        date: moment(Date.now()).format('YYYY-MM-DD HH:mm:ss')
    });
    comment.save().then(data => {
        res.json(data);
    });
}
exports.update = function (req, res, next) {
    var id = req.params.id;
    Comment.findByIdAndUpdate(id, {
        $set: req.body
    }, {
        new: false
    }).then(data => {
        res.json(data);
    });
}
exports.get2 = function (req, res, next) {
    var id = req.params.id;
    Comment.findById(id).then(data => {
        res.json(data);
    });
}
exports.delete = function (req, res, next) {
    var id = req.params.id;
    Comment.findByIdAndDelete(id).then(data => {
        res.json(data);
    });
}
exports.removes = function (req, res, next) {
    var ids = req.body.ids.split(',');
    Comment.remove({
        _id: {
            $in: ids
        }
    }).then(function () {
        res.json({
            'message': 'success',
            status: 200
        });
    });
}
exports.list = function (req, res, next) {
    var page = req.body.page ? req.body.page : 1;
    var rows = req.body.rows ? req.body.rows : 5;
    var cate = {};
    if (req.body.cateId && req.body.cateId.length > 0) {
        // getTreeD(req.body.cateId, page, rows, res);
        cate = {
            cateId: req.body.cateId
        }

    }
    Comment.paginate(cate, {
        page: +page,
        limit: +rows
    }, function (err, result) {
        result.rows = result.docs;
        delete result.docs;
        res.json(result);
    });
}

exports.getComment=function(req,res,next){
    var cateId=req.body.cateId;
    Comment.find({cateId:cateId}).count().then(data=>{
        var obj={
            "rows":[
                {
                    "total":data+"条"
                }
            ]
        }
        res.json(obj);
    });
}